<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="和你分享有用和有趣的知识">
  <meta name="keyword" content="hexo-theme, vuejs">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      MacOS修复Github不能通过ssh使用的问题 | 小迅的神奇海螺
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 4.2.1"></head>
<!-- <div class="wechat-share">
  <img src="/css/images/logo.png" />
</div> -->
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <!-- <div class="logo"></div> -->
      <span>小迅的神奇海螺</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">Projects</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">Projects</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>MacOS修复Github不能通过ssh使用的问题</h2>
  <p class="post-date">2021-10-19</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="MacOS修复Github不能通过ssh使用的问题"><a href="#MacOS修复Github不能通过ssh使用的问题" class="headerlink" title="MacOS修复Github不能通过ssh使用的问题"></a>MacOS修复Github不能通过ssh使用的问题</h1><h2 id="问题背景"><a href="#问题背景" class="headerlink" title="问题背景"></a>问题背景</h2><p>Github以安全为由，推荐以ssh方式替代https，项目遂换成ssh方式，但是由于22端口的连接性问题，经常会无法连接，提示连接超时</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">% git push</span><br><span class="line">kex_exchange_identification: Connection closed by remote host</span><br><span class="line">fatal: Could not <span class="built_in">read</span> from remote repository.</span><br><span class="line"></span><br><span class="line">Please make sure you have the correct access rights</span><br><span class="line">and the repository exists.</span><br></pre></td></tr></table></figure>
<h2 id="问题排查"><a href="#问题排查" class="headerlink" title="问题排查"></a>问题排查</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">% ssh -Tv git@github.com</span><br><span class="line">OpenSSH_8.1p1, LibreSSL 2.7.3</span><br><span class="line">debug1: Reading configuration data /etc/ssh/ssh_config</span><br><span class="line">debug1: /etc/ssh/ssh_config line 47: Applying options <span class="keyword">for</span> *</span><br><span class="line">debug1: Connecting to github.com port 22.</span><br><span class="line">debug1: Connection established.</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_rsa <span class="built_in">type</span> 0</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_rsa-cert <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_dsa <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_dsa-cert <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_ecdsa <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_ecdsa-cert <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_ed25519 <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_ed25519-cert <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_xmss <span class="built_in">type</span> -1</span><br><span class="line">debug1: identity file /Users/xiaoxun/.ssh/id_xmss-cert <span class="built_in">type</span> -1</span><br><span class="line">debug1: Local version string SSH-2.0-OpenSSH_8.1</span><br><span class="line">kex_exchange_identification: Connection closed by remote host</span><br></pre></td></tr></table></figure>

<h2 id="解决办法"><a href="#解决办法" class="headerlink" title="解决办法"></a>解决办法</h2><p>既然22端口不能用，那现在就有两种思路：</p>
<ul>
<li>1.设置ssh代理，把github的ssh连接代理到科学通道上；</li>
<li>2.换个没被封禁的端口</li>
</ul>
<p>两个方案都有一个前置条件：<strong>你需要有一个稳定的科学上网工具</strong></p>
<h3 id="方案1"><a href="#方案1" class="headerlink" title="方案1"></a>方案1</h3><p>参考了<a href="https://gist.github.com/chuyik/02d0d37a49edc162546441092efae6a1" target="_blank" rel="noopener">macOS 给 Git(Github) 设置代理（HTTP/SSH）</a>提供的方案：<br>修改<code>~/.ssh/config</code>文件（不存在则新建）：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"># macOS 给 Git(Github) 设置代理（HTTP&#x2F;SSH）</span><br><span class="line"># https:&#x2F;&#x2F;gist.github.com&#x2F;chuyik&#x2F;02d0d37a49edc162546441092efae6a1</span><br><span class="line"># 0x00 克隆 repo 的两种方式：https 和 ssh 方式</span><br><span class="line"># https 方式：git clone https:&#x2F;&#x2F;github.com&#x2F;owner&#x2F;git.git</span><br><span class="line"># ssh   方式：git clone     git@github.com:owner&#x2F;git.git</span><br><span class="line"></span><br><span class="line"># 0x01 https 方式克隆的 repo，走 http 或 sock5 代理，任选一个</span><br><span class="line"># 0x0101 http 代理</span><br><span class="line"># git config --global http.proxy &quot;http:&#x2F;&#x2F;127.0.0.1:1087&quot;</span><br><span class="line"># git config --global https.proxy &quot;http:&#x2F;&#x2F;127.0.0.1:1087&quot;</span><br><span class="line"># 0x0102 sock5 代理</span><br><span class="line"># git config --global http.proxy &quot;socks5:&#x2F;&#x2F;127.0.0.1:1086&quot;</span><br><span class="line"># git config --global https.proxy &quot;socks5:&#x2F;&#x2F;127.0.0.1:1086&quot;</span><br><span class="line"># 0x0103 取消使用代理</span><br><span class="line"># git config --global --unset http.proxy</span><br><span class="line"># git config --global --unset https.proxy</span><br><span class="line"></span><br><span class="line"># 0x02 ssh 克隆方式的代理设置，直接在全局设置文件配置，即 ~&#x2F;.ssh&#x2F;config 文件</span><br><span class="line">Host github.com</span><br><span class="line">   HostName github.com</span><br><span class="line">   User git</span><br><span class="line">   # 走 HTTP 代理，需要 brew install socat</span><br><span class="line">   # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport&#x3D;1087</span><br><span class="line">   # 走 socks5 代理（如 Shadowsocks）</span><br><span class="line">   ProxyCommand nc -v -x 127.0.0.1:1086 %h %p</span><br><span class="line">   # 走 socks5 代理（如 Shadowsocks），Windows 平台没有 nc 命令</span><br><span class="line">   # ProxyCommand connect -S 127.0.0.1:1086 %h %p</span><br></pre></td></tr></table></figure>
<p>走http代理需要<code>socat</code>这个工具，运行<code>brew install socat</code>命令安装它</p>
<h3 id="方案2"><a href="#方案2" class="headerlink" title="方案2"></a>方案2</h3><p>查到有大佬通过github的443端口进行ssh操作的案例，原文<a href="https://www.cnblogs.com/dayang12525/p/12509225.html" target="_blank" rel="noopener">github 的22端口无法连接的解决办法</a><br>修改<code>~/.ssh/config</code>文件（不存在则新建）：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Host github.com</span><br><span class="line">   HostName ssh.github.com</span><br><span class="line">   User yourmail@xx.com</span><br><span class="line">   Port 443</span><br></pre></td></tr></table></figure>

<h2 id="Windows下的解决方案"><a href="#Windows下的解决方案" class="headerlink" title="Windows下的解决方案"></a>Windows下的解决方案</h2><p>依然是<code>~/.ssh/config</code>文件，在前述方案下，添加<code>HostName</code>同级的配置，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 注意修改路径为你的路径</span><br><span class="line">IdentityFile &quot;C:\Users\xiaoxun\.ssh\id_rsa&quot;</span><br><span class="line">TCPKeepAlive yes</span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#错题集" >
    <span class="tag-code">错题集</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2021/09/23/%E6%AF%9B%E5%9D%AF%E6%88%BF%E6%94%B6%E6%88%BF%E6%94%BB%E7%95%A5/">
        <span class="nav-arrow">← </span>
        
          毛坯房收房攻略
        
      </a>
    
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Utterances START -->
      <div id="utterances"></div>
      <script src="https://utteranc.es/client.js"
        repo="7YHong/7YHong.github.io"
        issue-term="pathname"
        theme="github-light"
        crossorigin="anonymous"
        async></script>    
      <!-- Utterances END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#MacOS修复Github不能通过ssh使用的问题"><span class="toc-nav-text">MacOS修复Github不能通过ssh使用的问题</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#问题背景"><span class="toc-nav-text">问题背景</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#问题排查"><span class="toc-nav-text">问题排查</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#解决办法"><span class="toc-nav-text">解决办法</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#方案1"><span class="toc-nav-text">方案1</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#方案2"><span class="toc-nav-text">方案2</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#Windows下的解决方案"><span class="toc-nav-text">Windows下的解决方案</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://yoursite.com/2021/10/19/fix-github-push-via-ssh/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    Theme by <a href="https://github.com/yanm1ng" target="_blank" rel="noopener">yanm1ng</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->


<script src="/js/script.js"></script>


  </body>
</html>